#include "ion_thruster.h"

ErrorCode ion_thruster_init(IonThruster* thruster, const ThrusterSpec* spec) {
    if (!thruster || !spec) return ERROR_INVALID_INPUT;
    
    thruster->spec = *spec;
    thruster->state = THRUSTER_OFF;
    thruster->current_thrust = 0.0;
    thruster->commanded_thrust = 0.0;
    thruster->fuel_remaining = 50.0;  // 初始燃料50kg
    thruster->total_impulse = 0.0;
    thruster->operating_time = 0.0;
    thruster->temperature = 20.0;
    thruster->power_level = 0.0;
    thruster->is_available = true;
    
    return SUCCESS;
}

ErrorCode ion_thruster_command(IonThruster* thruster, double thrust_command) {
    if (!thruster) return ERROR_INVALID_INPUT;
    
    thrust_command = CLAMP(thrust_command, 0.0, thruster->spec.max_thrust);
    
    if (thruster->state == THRUSTER_OFF && thrust_command > 0.0) {
        thruster->state = THRUSTER_WARMUP;
        thruster->commanded_thrust = thrust_command;
    } else if (thruster->state == THRUSTER_READY) {
        thruster->commanded_thrust = thrust_command;
    } else if (thrust_command == 0.0 && thruster->state != THRUSTER_OFF) {
        thruster->state = THRUSTER_SHUTDOWN;
    }
    
    return SUCCESS;
}

ErrorCode ion_thruster_update(IonThruster* thruster, double dt) {
    if (!thruster || dt <= 0.0) return ERROR_INVALID_INPUT;
    
    switch (thruster->state) {
        case THRUSTER_OFF:
            thruster->current_thrust = 0.0;
            thruster->power_level = 0.0;
            thruster->temperature = MAX(20.0, thruster->temperature - 0.1 * dt);
            break;
            
        case THRUSTER_WARMUP:
            thruster->temperature += 5.0 * dt;
            if (thruster->temperature >= 200.0) {
                thruster->state = THRUSTER_READY;
            }
            break;
            
        case THRUSTER_READY:
            // 平滑过渡到指令推力
            double diff = thruster->commanded_thrust - thruster->current_thrust;
            thruster->current_thrust += diff * (1.0 - exp(-dt / thruster->spec.response_time));
            
            // 更新温度
            double load_factor = thruster->current_thrust / thruster->spec.max_thrust;
            thruster->temperature = 200.0 + 100.0 * load_factor;
            thruster->power_level = load_factor;
            
            // 消耗燃料
            double fuel_used = ion_thruster_fuel_consumption(thruster, thruster->current_thrust, dt);
            if (fuel_used > thruster->fuel_remaining) {
                thruster->state = THRUSTER_ERROR;
                return ERROR_CONTROL_FAILED;
            }
            
            thruster->fuel_remaining -= fuel_used;
            thruster->total_impulse += thruster->current_thrust * dt;
            thruster->operating_time += dt;
            break;
            
        case THRUSTER_SHUTDOWN:
            thruster->current_thrust *= 0.9;  // 指数衰减
            if (thruster->current_thrust < 0.001) {
                thruster->current_thrust = 0.0;
                thruster->state = THRUSTER_OFF;
            }
            break;
            
        case THRUSTER_ERROR:
            thruster->current_thrust = 0.0;
            thruster->is_available = false;
            break;
    }
    
    return SUCCESS;
}

ErrorCode ion_thruster_shutdown(IonThruster* thruster) {
    if (!thruster) return ERROR_INVALID_INPUT;
    
    thruster->state = THRUSTER_SHUTDOWN;
    thruster->commanded_thrust = 0.0;
    
    return SUCCESS;
}

ErrorCode ion_thruster_calculate_impulse(IonThruster* thruster, double duration, double* impulse) {
    if (!thruster || !impulse || duration <= 0.0) return ERROR_INVALID_INPUT;
    
    *impulse = thruster->current_thrust * duration;
    return SUCCESS;
}

double ion_thruster_fuel_consumption(IonThruster* thruster, double thrust, double duration) {
    if (!thruster || thrust <= 0.0 || duration <= 0.0) return 0.0;
    
    // 燃料消耗 = 推力 * 时间 / (比冲 * 重力加速度)
    return thrust * duration / (thruster->spec.specific_impulse * GRAVITY);
}

double ion_thruster_delta_v(IonThruster* thruster, double fuel_mass) {
    if (!thruster || fuel_mass <= 0.0) return 0.0;
    
    // 齐奥尔科夫斯基火箭方程
    return thruster->spec.specific_impulse * GRAVITY * log(SATELLITE_MASS / (SATELLITE_MASS - fuel_mass));
}

bool ion_thruster_is_ready(const IonThruster* thruster) {
    if (!thruster) return false;
    return thruster->state == THRUSTER_READY && thruster->is_available;
}

double ion_thruster_efficiency_at_thrust(const IonThruster* thruster, double thrust) {
    if (!thruster || thrust <= 0.0) return 0.0;
    
    // 效率随推力变化的模型
    double ratio = thrust / thruster->spec.max_thrust;
    return thruster->spec.efficiency * (0.8 + 0.2 * ratio);
}

void ion_thruster_print_status(const IonThruster* thruster) {
    if (!thruster) return;
    
    printf("=== 离子发动机状态 ===\n");
    printf("状态: ");
    switch (thruster->state) {
        case THRUSTER_OFF: printf("关闭\n"); break;
        case THRUSTER_WARMUP: printf("预热中\n"); break;
        case THRUSTER_READY: printf("就绪\n"); break;
        case THRUSTER_THROTTLE: printf("调节中\n"); break;
        case THRUSTER_SHUTDOWN: printf("关闭中\n"); break;
        case THRUSTER_ERROR: printf("错误\n"); break;
    }
    
    printf("当前推力: %.6f N\n", thruster->current_thrust);
    printf("指令推力: %.6f N\n", thruster->commanded_thrust);
    printf("剩余燃料: %.3f kg\n", thruster->fuel_remaining);
    printf("总冲量: %.3f N·s\n", thruster->total_impulse);
    printf("运行时间: %.1f s\n", thruster->operating_time);
    printf("温度: %.1f °C\n", thruster->temperature);
    printf("功率水平: %.1f %%\n", thruster->power_level * 100.0);
    printf("可用: %s\n", thruster->is_available ? "是" : "否");
}
